package ezCall;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MultipartMessage;
import javax.wireless.messaging.TextMessage;

import net.rim.blackberry.api.invoke.ApplicationArguments;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;
import net.rim.blackberry.api.invoke.PhoneArguments;
import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.AddressException;
import net.rim.blackberry.api.mail.MessagingException;
import net.rim.device.api.ui.component.Dialog;


final public class Phone_Lib 
{
	//CHCBARAM
    public static boolean Call_ToNumber( String NumberStr )
    {
    	if( NumberStr.length() > 0 )
    	{
    		// 이메일 주소면 리턴
    		if( NumberStr.indexOf("@") >= 0 ) return false;

    		PhoneArguments call = new PhoneArguments(PhoneArguments.ARG_CALL, NumberStr);    		
    		Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, call);
    		
    		return true;
    	}
    	else
    		return false;
    }	
    
    
    public static boolean SMS_ToNumber( String NumberStr )
    {                
        if( NumberStr.length() > 0 )
    	{
    		// 이메일 주소면 리턴
    		if( NumberStr.indexOf("@") >= 0 ) return false;

			try 
			{
				MessageConnection mc = (MessageConnection)Connector.open( "sms://" );
				
	    		TextMessage m = (TextMessage)mc.newMessage( MessageConnection.TEXT_MESSAGE );
	    		m.setAddress( "sms://"+NumberStr );
	    		m.setPayloadText( "" );
	    		Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( m ) );				
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		return true;
    	}
    	else
    		return false;
    }
   
    
    public static boolean MMS_ToNumber( String NumberStr )
    {                
        if( NumberStr.length() > 0 )
    	{
    		// 이메일 주소면 리턴
    		if( NumberStr.indexOf("@") >= 0 ) return false;

			
    		
			//try 
			//{
				//MessageConnection mc = (MessageConnection)Connector.open( "mms://" );
				
				//MultipartMessage m = (MultipartMessage)mc.newMessage( MessageConnection.MULTIPART_MESSAGE );
	    		//m.setAddress( "mms://"+NumberStr );
	    		//m.setPayloadText( "" );
	    	
    		MessageArguments  AppArg;
    		
    		AppArg = new MessageArguments( MessageArguments.ARG_NEW_MMS );
    		
    		Dialog.alert( MessageArguments.ARG_NEW_MMS );
    		
	    		Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( MessageArguments.ARG_NEW_MMS, NumberStr, "", "" ) );				
    		//Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( MessageArguments.ARG_NEW_MMS  ) );
			//} catch (IOException e) {
				// TODO Auto-generated catch block
			//	e.printStackTrace();
			//}
			 
			 
    		
    		return true;
    	}
    	else
    		return false;
    }
    
    
    public static boolean isEMAIL_Address( String AddrStr )
    {
    	boolean Ret = false;
    	
    	if( AddrStr.length() > 0 )
    	{
    		// 이메일 주소면 리턴
    		if( AddrStr.indexOf("@") >= 0 )
    		{
    			Ret = true;
    		}
    	}
    	
    	return Ret;
    }
    
    public static boolean EMAIL_ToAddress( String AddrStr, String NameStr )
    {                
        if( AddrStr.length() > 0 )
    	{
    		// 이메일 주소면 리턴
    		if( AddrStr.indexOf("@") < 0 ) return false;

			try 
			{
				
				net.rim.blackberry.api.mail.Message m = new net.rim.blackberry.api.mail.Message();
				
			    Address Addr_Email = new Address(AddrStr, NameStr);
					
					 Address[] addresses = {Addr_Email};
					    
					    try {
							m.addRecipients(net.rim.blackberry.api.mail.Message.RecipientType.TO , addresses);
						} catch (MessagingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					    //m.setContent("");
					    //m.setSubject("");

			    		Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( m ) );				
			    		

			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		return true;
    	}
    	else
    		return false;
    }    
    
    

    
}
